﻿namespace PirateMap.Models.cls
{
  public class Cell
  {
    public Cell()
    {
      CellType = CellTag.Wall;
    }
    public Cell(int x, int y, CellTag type, string mapId)
    {
      X = x;
      Y = y;
      CellType = type;
      MapId = mapId;
    }
    public Cell(int x, int y, CellTag type, string mapId, int strenght, DirectionTag direction, bool isClockwise = true)
      : this(x, y, type, mapId)
    {
      Strenght = strenght;
      IsClockwise = isClockwise;
      Direction = direction;
    }

    public int X { get; private set; }
    public int Y { get; private set; }

    public string MapId { get; private set; }

    public int? Strenght { get; private set; }
    public bool? IsClockwise { get; private set; }
    public DirectionTag? Direction { get; private set; }

    public CellTag CellType { get; private set; }

    public Cell Next { private set; get; }

    public bool IsNextMeteorBelt(Cell next)
    {
      return ReferenceEquals(next, Next);
    }

    public bool SetNextBlackHole(Cell next)
    {
      if (CellType != CellTag.BlackHole || next.CellType != CellTag.BlackHole) 
        return false;

      Next = next;
      return true;
    }
    public bool SetNextMeteorBelt(Cell next)
    {
      if (CellType != CellTag.MeteorBelt || next.CellType != CellTag.MeteorBelt)
        return false;

      Next = next;
      return true;
    }

    public Cell GetNextBlackHole()
    {
      if (CellType != CellTag.BlackHole)
        return null;

      return Next;
    }
    public Cell GetNextMeteorBelt()
    {
      if (CellType != CellTag.MeteorBelt)
        return null;

      var str = Strenght;
      var next = this;

      while (str-- > 0)
        next = next.Next;

      return next;
    }

    public override string ToString()
    {
      switch (CellType)
      {
        case CellTag.Alien:
          return "A";
        case CellTag.Earth:
          return "E";
        case CellTag.BlackHole:
          return "BH";
        case CellTag.MeteorBelt:
          return "MB" + (IsClockwise.Value ? "+" : "-") + Strenght.Value;
        case CellTag.Treasure:
          return "T";
        case CellTag.Moon:
          return "M";
        case CellTag.Wall:
          return "W";
      }

      return base.ToString();
    }
  }
}